package com.wzapp.sitposture.usercenter.entity;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.wzapp.sitposture.common.converter.DateConverterJson;
import com.wzapp.sitposture.common.converter.DefaultDateConverterJson;
import com.wzapp.sitposture.common.converter.JsonArrayConverterJson;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by qianglong on 2018/1/16.
 */
@Data
@Entity(name = "user_info")
public class User implements Serializable {

    private static final long serialVersionUID = -7597216835991659039L;
    public static final String OWNER = "owner";

    @Id
    @GeneratedValue(generator = "uuid_32")
    @GenericGenerator(name = "uuid_32", strategy = "uuid")
    @Column(length = 32)
    private String id;

    /**
     * 用户昵称
     */
    private String account;

    /**
     * 用户密码
     */
    private String password;

    @JsonIgnore
    private String salt;
    /**
     * 用户头像
     */
    private String avatar;
    /**
     * 用户等级
     */
    private Integer level;

    /**
     * 状态(1：启用  2：冻结  3：注销）
     */
    private Integer status;
    /**
     * 手机号码
     */
    private String mobile;

    @Column(updatable = false)
    @Convert(converter = DateConverterJson.class)
    private Long createTime;

    @Convert(converter = DateConverterJson.class)
    private Long updateTime;
    /**
     * 个人主页路由
     */
    private String router;
    /**
     * 性别: f/m/x
     */
    private String sex;

    /**
     * 生日
     */
    @Convert(converter = DefaultDateConverterJson.class)
    private Long birthDate;

    /**
     * 电子邮箱
     */
    private String email;

    /**
     * 身份证编码
     */
    private String identityCard;

    /**
     * 城市id
     */
    private String cityId;

    /**
     * 城市名称
     */
    private String cityName;

    /**
     * 简介
     */
    private String introduce;
    /**
     * 详细地址
     */
    private String address;

    @Transient
    private String token;

    /**
     * 是否首次注册用户
     */
    @Transient
    private Boolean newUser = false;

    @Transient
    private AccessToken accessToken;

    /**
     * 用户登录鉴权
     */
    @Transient
    private Set<GrantedAuthority> authorities;

    public Set<GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> userAuthotities = new HashSet<>();
        userAuthotities.add(new SimpleGrantedAuthority(OWNER));
        return userAuthotities;
    }

    /**
     * 推荐码：数字类型
     */
    private String recommendCode;

    /**
     * 父级id，即推荐人
     */
    private String parentId;

    /**
     * 环信账号：id,password,nickName,type=parent|student
     */
    @Column(name = "ring_account", nullable = true, columnDefinition = "longtext")
    @Convert(converter = JsonArrayConverterJson.class)
    private JSONArray ringAccount;
    /**
     * 是否ios用户，默认不是，ios只要登陆，就标志为1，ios上的app必须付费才能下载
     */
    private Integer iosUser = 0;

}
